﻿--[[
   @brief    混合消息列表model
   @author   rickonzhang
   @version  1.0
   @date     2016-06-27
--]]


contact_message_list_model =
{
    is_get_friend_list_done_ = false;
    is_get_group_list_done_ = false;

    message_banner_infomation_set = {};
    max_mode_root_id_ = 0;
    min_mode_root_id_ = 0;

    banner_id_to_banner_info = {};
    current_selected_info_id = "";
    current_hover_banner_id = 0;
};

function contact_message_list_model.init()
end

--必须是稳定排序
function contact_message_list_model.sort_fun(info_a, info_b)
    local time_a = 0;
    if info_a ~= nil and info_a.last_msg ~= nil then
        time_a = info_a.last_msg.time;
    end

    local time_b = 0;
    if info_b ~= nil and info_b.last_msg ~= nil then
        time_b = info_b.last_msg.time;
    end

    if time_a ~= time_b then
        return time_a > time_b;
    end

    if info_a == nil then
        return false;
    end

    if info_b == nil then
        return true;
    end

    if info_a.name ~= info_b.name then
        return info_a.name > info_b.name; --时间相等的情况下，按照名称排序
    else
        return info_a.id > info_b.id; --相等的情况下，按照id排序, id一定不同，所以会有结果, 保证稳定排序
    end
end

function contact_message_list_model.get_chat_count()
    --contact_message_list_model.message_banner_infomation_set = {} --todo 故意清空了，
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('contact_message_list_model.calc_list');
    tpfui:AddInterval('contact_message_list_model.calc_list', 2);
    if #contact_message_list_model.message_banner_infomation_set == 0 then
        contact_message_list_model.calc_list();
    end;
    
    return #contact_message_list_model.message_banner_infomation_set
end;

function contact_message_list_model.get_banner_info_list()
    if #contact_message_list_model.message_banner_infomation_set == 0 then
        return contact_message_list_model.calc_list();
    else
        return contact_message_list_model.message_banner_infomation_set;
    end;
end

function contact_message_list_model.get_banner_info_list_real_time()
    return contact_message_list_model.calc_list();
end

function contact_message_list_model.get_banner_info_by_index(index)
    if #contact_message_list_model.message_banner_infomation_set == 0 then
        contact_message_list_model.calc_list();
    else
        return contact_message_list_model.message_banner_infomation_set[index];
    end;
end;

function contact_message_list_model.calc_list()
    --clear model 

    --不要频繁更新list，下面这点代码做个判断要不要更新数据。

    if group_list_model.data_refreshed_ == nil then
        group_list_model.data_refreshed_ = {};
    end;
    if friend_list_model.data_refreshed_ == nil then
        friend_list_model.data_refreshed_ = {};
    end;

    if group_list_model.data_refreshed_['calc_list'] == nil or friend_list_model.data_refreshed_['calc_list'] == nil then
        group_list_model.data_refreshed_['calc_list'] = true;
        friend_list_model.data_refreshed_['calc_list'] = true;
    else
        return contact_message_list_model.message_banner_infomation_set;
    end;
    base_utility.log("[contact_message_list_model]auto_update_vlist");
    contact_message_list_model.message_banner_infomation_set = {};

    local friend_list = friend_list_model.get_friend_list();
    for k, f_info in pairs(friend_list) do
        if f_info.last_msg_ ~= nil and f_info.last_msg_.time ~= 0 then
            local info_for_banner = {};
            info_for_banner["type"]     = "friend";
            info_for_banner["id"]       = f_info.friend_id;
            info_for_banner["face_url"] = f_info.url;

            if nil ~= f_info.new_name and "" ~= f_info.new_name then
                info_for_banner["name"]     = f_info.new_name;
            else
                info_for_banner["name"]     = f_info.name;
            end
            

            info_for_banner["unread_msg_cnt"] = 0;
            if f_info.history_unread_msg_count ~= nil then
                info_for_banner["unread_msg_cnt"] = f_info.history_unread_msg_count;
            end

            info_for_banner["last_msg"] = {};
            info_for_banner["last_msg"]["time"] = f_info.last_msg_.time;


            local msg_text = social_utility.format_msg(f_info.last_msg_.message);
            if msg_text == nil then
                msg_text = "";
            end
            info_for_banner["last_msg"]["msg_text"] = msg_text;

            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            local formated_time_t = ierd_tgp.social_network.tagFormatedTime:new();
            social_network:format_time(f_info.last_msg_.time, formated_time_t);
            info_for_banner["last_msg"]["format_time"]  = formated_time_t.time_fromated
            formated_time_t:delete();

            table.insert(contact_message_list_model.message_banner_infomation_set, info_for_banner);
        end
    end

    local group_list = group_list_model.get_group_list();
    for k, g_info in pairs(group_list) do
        if g_info.last_msg_ ~= nil and g_info.last_msg_.time ~= 0 then
            local info_for_banner = {};
            info_for_banner["type"]     = "group";
            info_for_banner["id"]       = g_info.group_id;
            info_for_banner["face_url"] = g_info.face_url;
            info_for_banner["name"]     = g_info.name;
            info_for_banner["mask_flag"] = g_info.mask_msg_flag;
            info_for_banner["group_type"] = g_info.group_type;

            info_for_banner["unread_msg_cnt"] = 0;
            if g_info.history_unread_msg_count ~= nil then
                info_for_banner["unread_msg_cnt"] = g_info.history_unread_msg_count;
            end


            info_for_banner["last_msg"] = {};
            info_for_banner["last_msg"]["time"] = g_info.last_msg_.time;

            local msg_text = social_utility.format_msg(g_info.last_msg_.message);
            if g_info.last_msg_.msg_type == 1 then
                msg_text = social_utility.format_discussion_msg(g_info.last_msg_);
                if msg_text == nil then
                    msg_text = LoadStr("SOCIAL_MESSAGE_LIST_GROUPMSG");
                end
            end
            info_for_banner["last_msg"]["msg_text"] = msg_text;

            local social_network =
                base_utility.get_component_adapter("ISocial_network",
                    "ierd_tgp.social_network.Get_social_network_adapter();");
            local formated_time_t = ierd_tgp.social_network.tagFormatedTime:new();
            social_network:format_time(g_info.last_msg_.time, formated_time_t);
            info_for_banner["last_msg"]["format_time"] = formated_time_t.time_fromated
            formated_time_t:delete();

            table.insert(contact_message_list_model.message_banner_infomation_set, info_for_banner);
        end
    end

    table.sort(contact_message_list_model.message_banner_infomation_set, contact_message_list_model.sort_fun);

    return contact_message_list_model.message_banner_infomation_set;
end

contact_message_list_model.init();
